<?php

namespace app\topics\middle;

class UserMiddle extends \app\base\middle\BaseMiddle {

    private $info = [];

    private function getInfo() {
        if($this->info) {
            return $this->info;
        }
        $id = intval($this->params['id']);
        $info = target('topics/TopicsUser')->getWhereInfo([
            'user_id' => $id
        ]);
        if (empty($info)) {
            return [];
        }
        $jobList = target('topics/TopicsUser')->jobs();
        $info['job'] = $jobList[$info['job_id']];
        $this->info = $info;
        return $this->info;
    }

    protected function meta() {
        $this->setMeta('用户中心');
        $this->setName('用户中心');
        $this->setCrumb([
            [
                'name' => '用户中心',
                'url' => url()
            ]
        ]);
        $info = $this->getInfo();
        return $this->run([
            'pageInfo' => $this->pageInfo,
            'info' => $info
        ]);
    }

    protected function data() {
        $info = $this->getInfo();
        if(empty($info)) {
            return $this->stop('用户不存在!', 404);
        }
        $this->setMeta($info['show_name']);
        $this->setName($info['show_name']);
        $this->setCrumb([
            [
                'name' => $info['show_name'],
                'url' => url('', ['id' => $this->info['user_id']])
            ]
        ]);

        $detailList = target('topics/TopicsDetail')->loadList([
            'A.user_id' => $info['user_id']
        ], 10, 'A.create_time desc');

        $replyList = target('topics/TopicsDetailReply')->loadList([
            'A.user_id' => $info['user_id']
        ], 10, 'A.create_time desc');

        $pageParams = [];
        $pageParams['id'] = $this->info['user_id'];
        return $this->run([
            'info' => $info,
            'detailList' => $detailList,
            'replyList' => $replyList,
            'pageParams' => $pageParams
        ]);
    }

    protected function detail() {
        $info = $this->getInfo();
        if(empty($info)) {
            return $this->stop('用户不存在!', 404);
        }
        $this->setMeta('发表的主题 - '. $info['show_name']);
        $this->setName('发表的主题');
        $this->setCrumb([
            [
                'name' => $info['show_name'],
                'url' => url('index', ['id' => $this->info['user_id']])
            ],
            [
                'name' => '发表的主题',
                'url' => url('', ['id' => $this->info['user_id']])
            ]
        ]);
        $pageParams = [];
        $pageParams['id'] = $this->info['user_id'];
        $pageLimit = 20;
        $where['A.user_id'] = $this->info['user_id'];
        $model = target('topics/TopicsDetail');
        $count = $model->countList($where);
        $pageData = $this->pageData($count, $pageLimit);
        $list = $model->loadList($where, $pageData['limit'], 'A.create_time desc');

        return $this->run([
            'pageData' => $pageData,
            'countList' => $count,
            'pageList' => $list,
            'pageParams' => $pageParams
        ]);
    }

    protected function reply() {
        $info = $this->getInfo();
        if(empty($info)) {
            return $this->stop('用户不存在!', 404);
        }
        $this->setMeta('发布的回复 - '. $info['show_name']);
        $this->setName('发布的回复');
        $this->setCrumb([
            [
                'name' => $info['show_name'],
                'url' => url('index', ['id' => $this->info['user_id']])
            ],
            [
                'name' => '发布的回复',
                'url' => url('', ['id' => $this->info['user_id']])
            ]
        ]);
        $pageParams = [];
        $pageParams['id'] = $this->info['user_id'];
        $pageLimit = 20;
        $where['A.user_id'] = $this->info['user_id'];
        $model = target('topics/TopicsDetailReply');
        $count = $model->countList($where);
        $pageData = $this->pageData($count, $pageLimit);
        $list = $model->loadList($where, $pageData['limit'], 'A.create_time desc');

        return $this->run([
            'pageData' => $pageData,
            'countList' => $count,
            'pageList' => $list,
            'pageParams' => $pageParams
        ]);
    }


}